
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class IntroFrame extends JFrame implements MouseListener, ActionListener
{
	ImageIcon backImage = new ImageIcon("images/back.png");
	humanHuman humanHuman;
	humanRobot humanRobot;
	exit exit;

	boolean Insert = true;
	boolean Mill = false;
//	boolean Move = false;
	boolean Remove = false;
	boolean firstStoneClick=true;
	
	int initialX;
	int initialY;

	int i = 0;
	playersInfoEntry playersInfoEntry = new playersInfoEntry();
	singlePlayerInfoEntry singlePlayerInfoEntry = new singlePlayerInfoEntry();

	LabelContentPane contentPane = new LabelContentPane();

	BoardFrame boardFrame = new BoardFrame();

	PlayerG player0;
	PlayerG player1;

	Piece stone;
	static int countStone0;		// On Board stones
	static int countStone1;
	Piece [][] piece;

	Random rand = new Random();

	public IntroFrame()
	{
		super("Nine Men's Morris");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(contentPane);
		setJMenuBar(new MainMenu());
		setBounds(180,70,750,600);
		setResizable(false);
		setLayout(null);

		humanHuman = new humanHuman();
		humanHuman.addMouseListener(this);
		contentPane.add(humanHuman);
		System.out.println(humanHuman.isLightweight());

		humanRobot = new humanRobot();
		contentPane.add(humanRobot);
		humanRobot.addMouseListener(this);

		exit = new exit();
		contentPane.add(exit);
		exit.addMouseListener(this);
		playersInfoEntry.back.addMouseListener(this);
		playersInfoEntry.forward.addMouseListener(this);

		singlePlayerInfoEntry.back.addMouseListener(this);
		singlePlayerInfoEntry.forward.addMouseListener(this);

		singlePlayerInfoEntry.firstPlayerColor.addMouseListener(this);
		singlePlayerInfoEntry.secondPlayerColor.addMouseListener(this);

		boardFrame.addMouseListener(this);
		playersInfoEntry.firstPlayerColor.addMouseListener(this);
		playersInfoEntry.secondPlayerColor.addMouseListener(this);

		countStone0 = 0;
		countStone1 = 0;
		piece = new Piece[7][7];

	}

	public int mapX(int x, int y)
	{
//		if(validBoardPlace(x, y))
//		{
			if(x > 28 && x < 88 && y > 38 && y < 98)
			{
				return 0;
			}
			if(x > 225 && x < 285 && y > 38 && y < 98)
			{
				return 0;
			}
			if(x > 428 && x < 488 && y > 38 && y < 98)
			{
				return 0;
			}
			if(x > 72 && x < 132 && y > 87 && y < 147)
			{
				return 1;
			}
			if(x > 225 && x < 285 && y > 78 && y < 138)
			{
				return 1;
			}
			if(x > 381 && x < 441 && y > 90 && y < 150)
			{
				return 1;
			}
			if(x > 118 && x < 178 && y > 135 && y < 195)
			{
				return 2;
			}
			if(x > 225 && x < 285 && y > 128 && y < 188)
			{
				return 2;
			}
			if(x > 334 && x < 394 && y > 137 && y < 197)
			{
				return 2;
			}
			if(x > 8 && x < 68 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 57 && x < 117 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 107 && x < 167 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 343 && x < 403 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 392 && x < 452 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 443 && x < 503 && y > 246 && y < 306)
			{
				return 3;
			}
			if(x > 118 && x < 178 && y > 357 && y < 417)
			{
				return 4;
			}
			if(x > 225 && x < 285 && y > 362 && y < 422)
			{
				return 4;
			}
			if(x > 337 && x < 397 && y > 357 && y < 417)
			{
				return 4;
			}
			if(x > 70 && x < 130 && y > 402 && y < 462)
			{
				return 5;
			}
			if(x > 225 && x < 285 && y > 413 && y < 473)
			{
				return 5;
			}
			if(x > 384 && x < 444 && y > 402 && y < 462)
			{
				return 5;
			}
			if(x > 25 && x < 85 && y > 449 && y < 509)
			{
				return 6;
			}
			if(x > 225 && x < 285 && y > 463 && y < 523)
			{
				return 6;
			}
			if(x > 431 && x < 491 && y > 449 && y < 509)
			{
				return 6;
			}
//		}
		return 100;
	}

	public int mapY(int x, int y)
	{
//		if(validBoardPlace(x, y))
//		{
			if(x > 28 && x < 88 && y > 38 && y < 98)
			{
				return 0;
			}
			if(x > 225 && x < 285 && y > 38 && y < 98)
			{
				return 3;
			}
			if(x > 428 && x < 488 && y > 38 && y < 98)
			{
				return 6;
			}
			if(x > 72 && x < 132 && y > 87 && y < 147)
			{
				return 1;
			}
			if(x > 225 && x < 285 && y > 78 && y < 138)
			{
				return 3;
			}
			if(x > 381 && x < 441 && y > 90 && y < 150)
			{
				return 5;
			}
			if(x > 118 && x < 178 && y > 135 && y < 195)
			{
				return 2;
			}
			if(x > 225 && x < 285 && y > 128 && y < 188)
			{
				return 3;
			}
			if(x > 334 && x < 394 && y > 137 && y < 197)
			{
				return 4;
			}
			if(x > 8 && x < 68 && y > 246 && y < 306)
			{
				return 0;
			}
			if(x > 57 && x < 117 && y > 246 && y < 306)
			{
				return 1;
			}
			if(x > 107 && x < 167 && y > 246 && y < 306)
			{
				return 2;
			}
			if(x > 343 && x < 403 && y > 246 && y < 306)
			{
				return 4;
			}
			if(x > 392 && x < 452 && y > 246 && y < 306)
			{
				return 5;
			}
			if(x > 443 && x < 503 && y > 246 && y < 306)
			{
				return 6;
			}
			if(x > 118 && x < 178 && y > 357 && y < 417)
			{
				return 2;
			}
			if(x > 225 && x < 285 && y > 362 && y < 422)
			{
				return 3;
			}
			if(x > 337 && x < 397 && y > 357 && y < 417)
			{
				return 4;
			}
			if(x > 70 && x < 130 && y > 402 && y < 462)
			{
				return 1;
			}
			if(x > 225 && x < 285 && y > 413 && y < 473)
			{
				return 3;
			}
			if(x > 384 && x < 444 && y > 402 && y < 462)
			{
				return 5;
			}
			if(x > 25 && x < 85 && y > 449 && y < 509)
			{
				return 0;
			}
			if(x > 225 && x < 285 && y > 463 && y < 523)
			{
				return 3;
			}
			if(x > 431 && x < 491 && y > 449 && y < 509)
			{
				return 6;
			}
//		}
		return 100;
	}

	public int Randomize()
	{
		return rand.nextInt(7);
	}

	public boolean checkEmptyPosition(int row, int column)
	{
		if(piece[row][column] == null)
			return true;
		else
			return false;
	}

	public boolean checkLegalPosition(int x, int y)
	{
		if((x == 0 || x == 6) && (y == 0 || y == 3 || y == 6))
				return true;
		else
			if((x == 1 || x == 5) && (y == 1 || y == 3 || y == 5))
				return true;
		else
			if((x == 2 || x == 4) && (y == 2 || y == 3 || y == 4)) 
				return true;
		else
			if((x == 3) && (y == 0 || y == 1 || y == 2 || y == 4 || y == 5 || y == 6))
				return true;
		else
			if((y == 3) && (x == 0 || x == 1 || x == 2 || x == 4 || x == 5 || x == 6))
				return true;
		else
			return false;
	}

	public void insertStone(int x, int y, PlayerG p)
	{
		if(stone.firstPlayerStones != 0 || stone.secondPlayerStones != 0)
		{
			if(checkEmptyPosition(x, y) == true && checkLegalPosition(x, y) == true)
			{
				piece[x][y] = new Piece(x, y, p.getPlayerNumber(), p.getPlayerColor());
				boardFrame.add(piece[x][y]);
				boardFrame.repaint();
				System.out.println("Player: " + piece[x][y].getcolor() + " The number of remaining stones is: " + piece[x][y].getRemaining());

				if(p.getPlayerNumber() == 0)
					countStone0++;
				else
					countStone1++;
			}

			if(checkHorizontalMill(x,y))
			{
				Insert = false;
				Mill = true;
//				Move = false;
				Remove = true;
//				boardFrame.removeMouseListener(this);
			}
			if(checkVerticalMill(x,y))
			{
				Insert = false;
				Mill = true;
//				Move = false;
				Remove = true;
//				boardFrame.removeMouseListener(this);
			}

//		piece[x][y].addMouseListener(this);
		i++;
		}
//		if(stone.firstPlayerStones == 0 && stone.secondPlayerStones == 0)
//		{
//			for(int a = 0; a <7; a++)
//			{
//				for(int b = 0; b < 7; b++)
//				{
//					if(piece[a][b]!=null)
//						piece[a][b].removeMouseListener(this);
//				}
//			}
//			boardFrame.addMouseListener(this);
//			Insert = false;
////			Move = true;
//			Remove=false;
////			Remove = true;
//		}
	}

	public void removeStone(int x, int y, PlayerG p)
	{
		int countMills0 = 0;
		int countMills1 = 0;
		for(int i = 0; i < 7; i++)
		{
			for(int j = 0; j < 7; j++)
			{
				if(checkVerticalMill(i,j) || checkHorizontalMill(i,j))
				{
					if(p.getPlayerNumber() == 0)
						countMills0++;
					else
						countMills1++;
				}
			}
		}

		
		if(countMills0 == countStone0 || countMills1 == countStone1)
		{
			if(piece[x][y].getcolor() != p.getPlayerNumber())
			{
				boardFrame.remove(piece[x][y]);
				piece[x][y] = null;
				boardFrame.repaint();
				if(stone.firstPlayerStones != 0 || stone.secondPlayerStones != 0)
				{
					Insert = true;
				}
				else
				{
//					Move = true;
				}
				if(p.getPlayerNumber() == 0)
					countStone0--;
				else
					countStone1--;

				boardFrame.addMouseListener(this);
			}
		}
		else
		{
			if(piece[x][y].getcolor() != p.getPlayerNumber())
			{
				if((checkVerticalMill(x,y) == false) && (checkHorizontalMill(x,y) == false) && (checkLegalPosition(x,y)))
				{
					boardFrame.remove(piece[x][y]);
					piece[x][y] = null;
					Remove=false;
					boardFrame.repaint();
					if(stone.firstPlayerStones != 0 || stone.secondPlayerStones != 0)
					{
						Insert = true;
					}
					else
					{
//						Move = true;
					}
					if(p.getPlayerNumber() == 0)
						countStone0--;
					else
						countStone1--;

					boardFrame.addMouseListener(this);
				}
			}
		}
	}

	public int checkWinner()
	{
		if ((countStone0 + stone.firstPlayerStones) < 3)	// firstPlayerStones variable is added so that checkWinner() isn't applied
		{													// during the begining of the game when the player inserts his first stone
			return 1;
		}
		else
		{
			if((countStone1 + stone.secondPlayerStones) < 3)
			{
				return 0;
			}
		}

		int counter0 = 0;
		int counter1 = 0;
		try
		{
			for(int i = 0; i < 7; i++)
			{
				for(int j = 0; j < 7; j++)
				{
					if(piece[i][j].getcolor() == 0 && checkAllPossibleMoves(i,j) == false)
						counter0++;
					else
						if(piece[i][j].getcolor() == 1 && checkAllPossibleMoves(i,j) == false)
							counter1++;
					continue;
				}
			}
		}
		catch(NullPointerException e)
		{
		}

			if(countStone0 == counter0)
			{
				System.out.println(player1.getPlayerName()+ " is the WINNER");
				return 1;
			}
			else
				if(countStone1 == counter1)
				{
					System.out.println(player0.getPlayerName()+ " is the WINNER");
					return 0;
				}
				else
				{
					return -1;		// It's a draw so far
				}

		}

	public boolean checkPossibleMove(int x, int y, int x1, int y1)
	{
		int temp;
		int z = 0;

		if(x == 0 || x == 6 || y == 0 || y == 6)
			z = 3;
		if(x == 1 || x == 5 || y == 1 || y == 5)
			z = 2;
		if(x == 2 || x == 4 || y == 2 || y == 4)
			z = 1;
		if(x == 3)
		{
			try
			{
				temp = y + 1;
				if(piece[x][temp] == null && (x1 == x && y1 == temp))
					return true;
			}
			catch(ArrayIndexOutOfBoundsException e){}
			
			try
			{
				temp = y - 1;
				if(piece[x][temp] == null && (x1 == x && y1 == temp))
					return true;
			}
			catch(ArrayIndexOutOfBoundsException e){}
		}

		if(y == 3)
		{
			try
			{
				temp = x + 1;
				if(piece[temp][y] == null && (x1 == temp && y1 == y))
					return true;
			}
			catch(ArrayIndexOutOfBoundsException e){}
			
			try
			{
				temp = x - 1;
				if(piece[temp][y] == null && (x1 == temp && y1 == y))
					return true;
			}
			catch(ArrayIndexOutOfBoundsException e){}
		}

		try
		{
			temp = y + z;
			if(piece[x][temp] == null && (x1 == x && y1 == temp))
				return true;
		}
		catch(ArrayIndexOutOfBoundsException e){}

		try
		{
			temp = x + z;
			if(piece[temp][y] == null && (x1 == temp && y1 == temp))
				return true;
		}
		catch(ArrayIndexOutOfBoundsException e){}

		try
		{
			temp = y - z;
			if(piece[x][temp] == null && (x1 == x && y1 == temp))
				return true;
		}
		catch(ArrayIndexOutOfBoundsException e){}

		try
		{
			temp = x - z;
			if(piece[temp][y] == null  && (x1 == temp && y1 == y))
				return true;
		}
		catch(ArrayIndexOutOfBoundsException e){}

		return false;
	}

	public boolean Move(int x, int y, int x1, int y1, PlayerG p)
	{
		if((checkEmptyPosition(x1, y1) == true) && (checkLegalPosition(x1, y1) == true) && (checkPossibleMove(x, y, x1, y1) == true) && (piece[x][y].getcolor() == p.getPlayerNumber()))
		{
			piece[x1][y1] = piece[x][y];
			piece[x][y] = null;
			piece[x1][y1].setPosition(x1, y1);
			boardFrame.repaint();
		}
			
		if(checkVerticalMill(x1,y1) || checkHorizontalMill(x1,y1))
			return true;
		else
			return false;		
	}

	public boolean checkAllPossibleMoves(int x, int y)
	{
		int z = 0;

		if((x == 0 || x == 6) || (y == 0 || y == 6))
			z = 3;

		if((x == 1 || x == 5) || (y == 1 || y == 5))
			z = 2;

		if((x == 2 || x == 4) || (y == 2 || y == 4))
			z = 1;

		// Couldn't think of any other way .. screw it for now
		try
        {if(piece[x][y + z] == null)      return true;}		// tries if there is an empty space near a stone then return true if caught an
        catch(ArrayIndexOutOfBoundsException e){}			// array out of bound exception just skip the check

        try
        {if(piece[x + z][y] == null)      return true;}
        catch(ArrayIndexOutOfBoundsException e){}

        try
        {if(piece[x][y - z] == null)      return true;}
        catch(ArrayIndexOutOfBoundsException e){}

        try
        {if(piece[x - z][y] == null)      return true;}
        catch(ArrayIndexOutOfBoundsException e){}

        return false;
	}

	public boolean checkHorizontalMill(int x, int y)
	{
		try
		{
			int z = 0;
			int w = 0;
			if(x == 0 || x == 6)
			{
				w = 0; 
				z = 3;
			}
		else if(x == 1 || x == 5)
		{
			w = 1;
			z = 2;
		}
		else if(x == 2 || x == 4)
		{
			w = 2;
			z = 1;
		}

		// Row 3, Column 3
		if(x == 3 && y < 3)
		{
			if((piece[3][0].getColor() == piece[3][1].getColor()) && (piece[3][0].getColor() == piece[3][2].getColor()))
			{
				return true;
			}
		}
		else if(x == 3 && y > 3)
		{
			if((piece[3][4].getColor() == piece[3][5].getColor()) && (piece[3][4].getColor() == piece[3][6].getColor()))
			{
				return true;
			}
		}

		// 2 stones, check third
		if((piece[x][w].getColor()) == (piece[x][3].getColor()))
		{
			z = w + (2*z);
			if((piece[x][z].getColor() == piece[x][3].getColor()))
			{
				return true;
			}
			else
				return false;
		}
		else
			return false;
		}

		catch(NullPointerException e)
		{
			return false;
		}
	}

	public boolean checkVerticalMill(int x, int y)
	{
		try
		{
			int w = 0;
			int z = 0;
			if(y == 0 || y == 6)
			{
				w = 0; 
				z = 3;
			}
			else if(y == 1 || y == 5)
			{
				w = 1; 
				z = 2;
			}
			else if(y == 2 || y == 4)
			{ 
				w = 2; 
				z = 1;
			}
			else if(y == 3 && x < 3)
			{
				if((piece[0][3].getColor() == piece[1][3].getColor()) && (piece[0][3].getColor() == piece[2][3].getColor()))
				{System.out.print("");return true;}
			}
			else if(y == 3 && x > 3)
			{
				if((piece[4][3].getColor() == piece[5][3].getColor()) && (piece[4][3].getColor() == piece[6][3].getColor()))
				{System.out.print("");return true;}
			}
			if((piece[w][y].getColor() == piece[3][y].getColor()))
			{
				z = w + (z*2);
				if(piece[z][y].getColor() == piece[3][y].getColor())
				{
					return true;
				}
				else
					return false;
			}
			else
				return false;
		}
		catch(NullPointerException e)
		{
			return false;
		}
	}

	public boolean validBoardPlace(int x, int y)
	{
		if((x > 28 && x < 88 && y > 38 && y < 98) || (x > 225 && x < 285 && y > 38 && y < 98) || 
				(x > 428 && x < 488 && y > 38 && y < 98) ||
				(x > 72 && x < 132 && y > 87 && y < 147) || (x > 225 && x < 285 && y > 78 && y < 138) ||
				(x > 381 && x < 441 && y > 90 && y < 150) || (x > 118 && x < 178 && y > 135 && y < 195) ||
				(x > 225 && x < 285 && y > 128 && y < 188) || (x > 334 && x < 394 && y > 137 && y < 197) ||
				(x > 8 && x < 68 && y > 246 && y < 306) || (x > 57 && x < 117 && y > 246 && y < 306) ||
				(x > 107 && x < 167 && y > 246 && y < 306) || (x > 343 && x < 403 && y > 246 && y < 306) ||
				(x > 392 && x < 452 && y > 246 && y < 306) || (x > 443 && x < 503 && y > 246 && y < 306) ||
				(x > 118 && x < 178 && y > 357 && y < 417) || (x > 225 && x < 285 && y > 362 && y < 422) ||
				(x > 337 && x < 397 && y > 357 && y < 417) || (x > 70 && x < 130 && y > 402 && y < 462) || 
				(x > 225 && x < 285 && y > 413 && y < 473) || (x > 384 && x < 444 && y > 402 && y < 462) ||
				(x > 25 && x < 85 && y > 449 && y < 509) || (x > 225 && x < 285 && y > 463 && y < 523) || 
				(x > 431 && x < 491 && y > 449 && y < 509))
			return true;
		else
			return false;
	}

	public void mousePressed(MouseEvent e)
	{
	}

	public int xStoneCoor(int x, int y)
	{
		if(x == 0 && y == 0)
			return 20;

		if(x == 0 && y == 3)
			return 221;
		
		if(x == 0 && y == 6)
			return 425;
		
		if(x == 1 && y == 1)
			return 63;
		
		if(x == 1 && y == 3)
			return 221;
		
		if(x == 1 && y == 5)
			return 370;
		
		if(x == 2 && y == 2)
			return 115;

		if(x == 2 && y == 3)
			return 221;

		if(x == 2 && y == 4)
			return 325;

		if(x == 3 && y == 0)
			return 5;

		if(x == 3 && y == 1)
			return 53;

		if(x == 3 && y == 2)
			return 103;

		if(x == 3 && y == 4)
			return 338;

		if(x == 3 && y == 5)
			return 388;

		if(x == 3 && y == 6)
			return 438;

		if(x == 4 && y == 2)
			return 110;

		if(x == 4 && y == 3)
			return 221;

		if(x == 4 && y == 4)
			return 326;

		if(x == 5 && y == 1)
			return 67;

		if(x == 5 && y == 3)
			return 221;

		if(x == 5 && y == 5)
			return 374;

		if(x == 6 && y == 0)
			return 23;

		if(x == 6 && y == 3)
			return 221;

		if(x == 6 && y == 6)
			return 421;

		return 0;
	}

	public int yStoneCoor(int x, int y)
	{
		if(x == 0 && y == 0)
			return 20;

		if(x == 0 && y == 3)
			return 7;
		
		if(x == 0 && y == 6)
			return 20;
		
		if(x == 1 && y == 1)
			return 68;
		
		if(x == 1 && y == 3)
			return 50;
		
		if(x == 1 && y == 5)
			return 60;
		
		if(x == 2 && y == 2)
			return 115;

		if(x == 2 && y == 3)
			return 100;

		if(x == 2 && y == 4)
			return 110;

		if(x == 3 && y == 0)
			return 220;

		if(x == 3 && y == 1)
			return 220;

		if(x == 3 && y == 2)
			return 220;

		if(x == 3 && y == 4)
			return 220;

		if(x == 3 && y == 5)
			return 220;

		if(x == 3 && y == 6)
			return 220;

		if(x == 4 && y == 2)
			return 330;

		if(x == 4 && y == 3)
			return 335;

		if(x == 4 && y == 4)
			return 328;

		if(x == 5 && y == 1)
			return 375;

		if(x == 5 && y == 3)
			return 385;

		if(x == 5 && y == 5)
			return 373;

		if(x == 6 && y == 0)
			return 423;

		if(x == 6 && y == 3)
			return 430;

		if(x == 6 && y == 6)
			return 420;
	

		
		return -1;
	}
	
	public void mouseReleased(MouseEvent e)
	{
//		if(e.getSource() == piece[0][0])
//		{
//			piece[0][0].removeMouseListener(this);
			System.out.println(e.getX() + " " + e.getY());
//		}
		if(Insert == true)
		{
			if(e.getSource() == boardFrame && validBoardPlace(e.getX(), e.getY()))
			{
				if((i % 2) == 0)
				{
					insertStone(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY()), player0);
					piece[mapX(e.getX(), e.getY())][mapY(e.getX(), e.getY())].setBounds(xStoneCoor(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())), yStoneCoor(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())), 60, 60);
				}
				else
				{
					insertStone(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY()), player1);
					piece[mapX(e.getX(), e.getY())][mapY(e.getX(), e.getY())].setBounds(xStoneCoor(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())), yStoneCoor(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())), 60, 60);
				}
			}
		}
		else
			if(Remove = true)
			{
				if(i % 2 != 0)
				{
					if(e.getSource() == piece[0][0])
					{
						removeStone(0, 0, player0);
					}
					if(e.getSource() == piece[0][3])
					{
						removeStone(0, 3, player0);	
					}
					if(e.getSource() == piece[0][6] )
					{
						removeStone(0, 6, player0);	
					}
					if(e.getSource() == piece[1][1])
					{
						removeStone(1, 1, player0);	
					}
					if(e.getSource() == piece[1][3])
					{
						removeStone(1, 3, player0);	
					}
					if(e.getSource() == piece[1][5])
					{
						removeStone(1, 5, player0);	
					}
					if(e.getSource() == piece[2][2])
					{
						removeStone(2, 2, player0);	
					}
					if(e.getSource() == piece[2][3])
					{
						removeStone(2, 3, player0);	
					}
					if(e.getSource() == piece[2][4])
					{
						removeStone(2, 4, player0);	
					}
					if(e.getSource() == piece[3][0])
					{
						removeStone(3, 0, player0);	
					}
					if(e.getSource() == piece[3][1])
					{
						removeStone(3, 1, player0);	
					}
					if(e.getSource() == piece[3][2])
					{
						removeStone(3, 2, player0);	
					}
					if(e.getSource() == piece[3][4])
					{
						removeStone(3, 4, player0);	
					}
					if(e.getSource() == piece[3][5])
					{
						removeStone(3, 5, player0);	
					}
					if(e.getSource() == piece[3][6])
					{
						removeStone(3, 6, player0);	
					}
					if(e.getSource() == piece[4][2])
					{
						removeStone(4, 2, player0);	
					}
					if(e.getSource() == piece[4][3])
					{
						removeStone(4, 3, player0);	
					}
					if(e.getSource() == piece[4][4])
					{
						removeStone(4, 4, player0);	
					}
					if(e.getSource() == piece[5][1])
					{
						removeStone(5, 1, player0);	
					}
					if(e.getSource() == piece[5][3])
					{
						removeStone(5, 3, player0);	
					}
					if(e.getSource() == piece[5][5])
					{
						removeStone(5, 5, player0);	
					}
					if(e.getSource() == piece[6][0])
					{
						removeStone(6, 0, player0);	
					}
					if(e.getSource() == piece[6][3])
					{
						removeStone(6, 3, player0);	
					}
					if(e.getSource() == piece[6][6])
					{
						removeStone(6, 6, player0);	
					}
				}
				else
				{
					if(e.getSource() == piece[0][0] )
					{
						removeStone(0, 0, player1);
					}
					if(e.getSource() == piece[0][3])
					{
						removeStone(0, 3, player1);	
					}
					if(e.getSource() == piece[0][6] )
					{
						removeStone(0, 6, player1);	
					}
					if(e.getSource() == piece[1][1])
					{
						removeStone(1, 1, player1);	
					}
					if(e.getSource() == piece[1][3])
					{
						removeStone(1, 3, player1);	
					}
					if(e.getSource() == piece[1][5])
					{
						removeStone(1, 5, player1);	
					}
					if(e.getSource() == piece[2][2])
					{
						removeStone(2, 2, player1);	
					}
					if(e.getSource() == piece[2][3])
					{
						removeStone(2, 3, player1);	
					}
					if(e.getSource() == piece[2][4])
					{
						removeStone(2, 4, player1);	
					}
					if(e.getSource() == piece[3][0])
					{
						removeStone(3, 0, player1);	
					}
					if(e.getSource() == piece[3][1])
					{
						removeStone(3, 1, player1);	
					}
					if(e.getSource() == piece[3][2])
					{
						removeStone(3, 2, player1);	
					}
					if(e.getSource() == piece[3][4])
					{
						removeStone(3, 4, player1);	
					}
					if(e.getSource() == piece[3][5])
					{
						removeStone(3, 5, player1);	
					}
					if(e.getSource() == piece[3][6])
					{
						removeStone(3, 6, player1);	
					}
					if(e.getSource() == piece[4][2])
					{
						removeStone(4, 2, player1);	
					}
					if(e.getSource() == piece[4][3])
					{
						removeStone(4, 3, player1);	
					}
					if(e.getSource() == piece[4][4])
					{
						removeStone(4, 4, player1);	
					}
					if(e.getSource() == piece[5][1])
					{
						removeStone(5, 1, player1);	
					}
					if(e.getSource() == piece[5][3])
					{
						removeStone(5, 3, player1);	
					}
					if(e.getSource() == piece[5][5])
					{
						removeStone(5, 5, player1);	
					}
					if(e.getSource() == piece[6][0])
					{
						removeStone(6, 0, player1);	
					}
					if(e.getSource() == piece[6][3])
					{
						removeStone(6, 3, player1);	
					}
					if(e.getSource() == piece[6][6])
					{
						removeStone(6, 6, player1);	
					}
				}
			}
//		else
//			if(Move == true)
//			{
//				System.out.println("A7A 1");
//				if(checkWinner()!=-1)
//				{
//					Move=false;
//					firstStoneClick=false;
//				}
//				if(firstStoneClick)
//				{
//		/*				System.out.println(checkAllPossibleMoves(xposition(e.getX()), yposition(e.getY())));
//						System.out.println("ALASHNA WA7DA");*/
//					if(i%2==0)
//					{
//						System.out.println("A7A 2");
//						int x1= mapX(e.getX(),e.getY());
//						int y1= mapY(e.getX(),e.getY());
//						if(piece[x1][y1]!=null)
//						{
//							System.out.println(piece[x1][y1].getBackground());
//							if(piece[x1][y1].getForeground()==player0.getPlayerColor())
//							{
//								if(checkAllPossibleMoves(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())))
//								{
//									System.out.println("A7A 3");
//										initialX=mapX(e.getX(), e.getY());
//										initialY=mapY(e.getX(), e.getY());
//										firstStoneClick=false;
//									}
//								}
//							}
//						}
//						else
//						{
//							int x1= mapX(e.getX(), e.getY());
//							int y1= mapY(e.getX(),e.getY());
//							if(piece[x1][y1]!=null)
//							{
//								if(piece[x1][y1].getForeground()==player1.getPlayerColor())
//								{
//									if(checkAllPossibleMoves(mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())))
//									{
//										System.out.println("A7A 4");
//										initialX=mapX(e.getX(), e.getY());
//										initialY=mapY(e.getX(), e.getY());
//										firstStoneClick=false;
//									}
//								}
//							}
//						}
//					}
//					else
//					{
//		/*				System.out.println("Initial Y: "+ initialY + "   Initial X: " + initialX);
//						System.out.println("Final Y: " + yposition(e.getY()) + "	Final X: " + xposition(e.getX()));
//						System.out.println("Can Move??? : " + checkPossibleMove(initialY, initialX, yposition(e.getY()), xposition(e.getX())));*/
//						if(checkPossibleMove(initialY, initialX, mapX(e.getX(), e.getY()), mapY(e.getX(), e.getY())))
//						{
//							System.out.println("A7A 5");
//							int tempx=mapX(e.getX(), e.getY());
//							int tempy=mapY(e.getX(),e.getY());
//		/*					System.out.println("TempX: "  +tempx + "\t\tTempY: " + tempy);
//							System.out.println(board[tempy][tempx]);
//							System.out.println(board[tempx][tempy]);*/
////							piece[tempx][tempy]=new JBu();
//							piece[tempx][tempy]=piece[initialY][initialX];
//							piece[initialY][initialX]=null;
//					//		piece[tempy][tempx].setLocation(xStoneCoor(tempx), yStoneCoor(tempy));
//							getContentPane().repaint();
//							if(checkVerticalMill(tempy, tempx))
//							{
//								Move=false;
//								if(i%2==0)
//								{
//									Remove=true;
//									countStone0--;
//								}
//								else
//								{
//									Remove=true;
//									countStone0--;
//								}
//								
//							}
//							if(checkHorizontalMill(tempy, tempx))
//							{
//								Move=false;
//								if(i%2==0)
//								{
//									Remove=true;
//									countStone1--;
//								}
//								else
//								{
//									Remove=true;
//									countStone1--;
//								}
//							}
//							i++;
//							firstStoneClick=true;
//						}
//						else
//							firstStoneClick=true;
//					}
//				}

		if(e.getSource() == singlePlayerInfoEntry.firstPlayerColor)
		{
			singlePlayerInfoEntry.firstColor = JColorChooser.showDialog(singlePlayerInfoEntry.firstPlayerColor,"Choose Stone Color",null);
			singlePlayerInfoEntry.firstPlayerName.setBackground(singlePlayerInfoEntry.firstColor);
		}

		if(e.getSource() == singlePlayerInfoEntry.secondPlayerColor)
		{
			singlePlayerInfoEntry.secondColor = JColorChooser.showDialog(singlePlayerInfoEntry.secondPlayerColor,"Choose Stone Color",null);
			singlePlayerInfoEntry.secondPlayerName.setBackground(singlePlayerInfoEntry.secondColor);
		}

		if(e.getSource() == playersInfoEntry.firstPlayerColor)
		{
			playersInfoEntry.firstColor = JColorChooser.showDialog(playersInfoEntry.firstPlayerColor,"Choose Stone Color",null);
			playersInfoEntry.firstPlayerName.setBackground(playersInfoEntry.firstColor);
		}

		if(e.getSource() == playersInfoEntry.secondPlayerColor)
		{
			playersInfoEntry.secondColor = JColorChooser.showDialog(playersInfoEntry.secondPlayerColor,"Choose Stone Color",null);
			playersInfoEntry.secondPlayerName.setBackground(playersInfoEntry.secondColor);
		}

		if(e.getSource() == humanHuman && humanHuman.isEnabled() == true)
		{
			contentPane.add(playersInfoEntry);
			humanRobot.setEnabled(false);
			playersInfoEntry.setVisible(true);
		}
		else
		{
			if(e.getSource() == humanRobot && humanRobot.isEnabled() == true)
			{
				contentPane.add(singlePlayerInfoEntry);
				humanHuman.setEnabled(false);
				singlePlayerInfoEntry.setVisible(true);
			}
			else
			{
				if(e.getSource() == playersInfoEntry.back)
				{
					playersInfoEntry.setVisible(false);
					humanHuman.setVisible(true);
					humanHuman.setEnabled(true);
					humanRobot.setEnabled(true);
				}
				else
				{
					if(e.getSource() == playersInfoEntry.forward)
					{
						setVisible(false);
						boardFrame.setVisible(true);
						boardFrame.setStatus(playersInfoEntry);
						player0 = new PlayerG(playersInfoEntry.firstPlayerName.getText(), 0, playersInfoEntry.firstColor);
						player1 = new PlayerG(playersInfoEntry.secondPlayerName.getText(), 1, playersInfoEntry.secondColor);
					}
					else
					{
						if(e.getSource() == singlePlayerInfoEntry.forward)
						{
							setVisible(false);
							boardFrame.setVisible(true);
							boardFrame.setStatus(singlePlayerInfoEntry);
							player0 = new PlayerG(singlePlayerInfoEntry.firstPlayerName.getText(), 0, singlePlayerInfoEntry.firstColor);
							player1 = new PlayerG(singlePlayerInfoEntry.secondPlayerName.getText(), 1, singlePlayerInfoEntry.secondColor);
						}
						else
						{
							if(e.getSource() == singlePlayerInfoEntry.back)
							{
								singlePlayerInfoEntry.setVisible(false);
								humanRobot.setVisible(true);
								humanHuman.setEnabled(true);
								humanRobot.setEnabled(true);
							}
						}
					}
				}
			}
		}
	}

	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	public void mouseClicked(MouseEvent e)
	{
	}

	public void actionPerformed(ActionEvent e)
	{
	}

}
